<template>
  <view class="my_settle_contanier">
   <label class="radio"  @click="toggleAll">
     <radio :checked="selectAllCart" color="#C00000" /><text>全选</text>
   </label>
   <view class="settle_total">
     合计：
     <text class="amount">￥{{selectPrice.toFixed(2)}}</text>
   </view>
   
   <view class="btn_settle" @click="gotoPay">
     结算({{selectCount}})
   </view>
  </view>
</template>

<script>
  import {mapGetters,mapMutations,mapState} from 'vuex'
  export default {
    name:"mysettle",
    computed:{
      ...mapGetters('cart',['selectAllCart','selectCount','selectPrice','selectCheckCart']),
      ...mapGetters('user',['getAddressName']),
      ...mapState('user',['token']),
      ...mapState('cart',['cart'])
    },
    data() {
      return {
        
      };
    },
    methods:{
      ...mapMutations('cart',['toggleAllCheck']),
      ...mapMutations('user',['updateRedirectInfo']),
      toggleAll(){
        // this.commit('')
        this.toggleAllCheck(!this.selectAllCart)
      },
      gotoPay(){
        if(!this.token) {
          this.showLogin()
          return
        }
        if(!this.getAddressName) return uni.$showMsg('您还未选择收货地址!')
        if(!this.selectCheckCart) return uni.$showMsg('您还未选择商品')
        this.payOrder()
        // uni.navigateTo({
        //   url:'/subpkg/pay/pay'
        // })
      },
      async payOrder(){
        const orderInfo={
          // order_price:this.selectPrice,
          order_price:0.01,
          consignee_addr:this.getAddressName,
          goods:this.cart.filter(item=>item.goods_state).map(item=>({
            goods_id:item.goods_id,
            goods_number:item.goods_count,
            goods_price:item.goods_price
          }))
        }
        const {data:res} =await uni.$http.post('/api/public/v1/my/orders/create',orderInfo)
        // console.log(res);
        if(res.meta.status!==200) return uni.$showMsg(res.meta.msg+'只能到这一步了wuwu')
        const orderNumber=res.message.order_num
        const {data:perOrder} =await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder',orderNumber)
        if(perOrder.meta.status!==200) return uni.$showMsg(perOrder.meta.msg)
        const payInfo=perOrder.message.pay
        const [err,succ] =await uni.requestPayment(payInfo)
         // 3.2 未完成支付
         if (err) return uni.$showMsg('订单未支付！')
         // 3.3 完成了支付，进一步查询支付的结果
         const { data: checkOrder } = await uni.$http.post('/api/public/v1/my/orders/chkOrder', { order_number: orderNumber })
         // 3.4 检测到订单未支付
         if (checkOrder.meta.status !== 200) return uni.$showMsg('订单未支付！')
         // 3.5 检测到订单支付完成
         uni.showToast({
           title: '支付完成！',
           icon: 'success'
         })
      },
     async showLogin(){
        const [err,succ]=await uni.showModal({
          title:'提示',
          content:'您还未登录'
        }).catch(err=>err)
        if(succ&&succ.confirm) {
          uni.switchTab({
            url:'/pages/my/my',
            success:()=>{
              this.updateRedirectInfo({
               openType:'switchTab',
               from:'/pages/cart/cart'
              })
            }
          })
        }
      }
    }
  }
</script>

<style lang="scss">
.my_settle_contanier {
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: fixed;
  bottom: 0;
  left: 0;
  background-color: #ffffff;
  font-size: 14px;
  padding-left: 5px;
  height: 50px;
  width: 100%;
  z-index: 999;
  .radio {
    display: flex;
    align-items: center;
  }
  .settle_total {
    .amount {
      color: #C00000;
      font-weight: 700;
    }
  }
  .btn_settle{
    background-color: #C00000;
    height: 50px;
    line-height: 50px;
    text-align: center;
    padding: 0 10px;
    min-width: 100px;
    color: #ffffff;
  }
}
</style>